//
//  NSString+Extension.m
//  LableTest
//
//  Created by 刘家俊 on 2017/7/12.
//  Copyright © 2017年 Andrew. All rights reserved.
//

#import "NSString+Extension.h"

@implementation NSString (Extension)

/** 两个string同种颜色不同字号 */
+ (NSMutableAttributedString *)ym_setAttribute:(NSString *)firstStr secondString:(NSString *)secondStr formerFont:(UIFont *)firstFont latterFont:(UIFont *)secondFont color:(UIColor *)color
{
    NSString *string = [NSString stringWithFormat:@"%@%@",firstStr,secondStr];
    NSMutableAttributedString *attrrSting = [[NSMutableAttributedString alloc] initWithString:string];
    
    NSUInteger firstLength = [firstStr length];
    NSUInteger secondLength = [secondStr length];
    
    [attrrSting addAttribute:NSFontAttributeName value:firstFont range:NSMakeRange(0, firstLength)];
    [attrrSting addAttribute:NSFontAttributeName value:secondFont range:NSMakeRange(firstLength, secondLength)];
    [attrrSting addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, firstLength + secondLength)];
    
    return attrrSting;
}

/** 两个string不同颜色不同字号 */
+ (NSMutableAttributedString *)ym_setattributes:(NSString *)firstStr secondString:(NSString *)secondStr formerFont:(UIFont *)firstFont latterFont:(UIFont *)secondFont firstColor:(UIColor *)firstColor secondColor:(UIColor *)secondColor
{
    NSString *string = [NSString stringWithFormat:@"%@%@",firstStr,secondStr];
    NSMutableAttributedString *attrSting = [[NSMutableAttributedString alloc] initWithString:string];
    
    NSUInteger firstLength = [firstStr length];
    NSUInteger secondLength = [secondStr length];
    
    [attrSting addAttribute:NSFontAttributeName value:firstFont range:NSMakeRange(0, firstLength)];
    [attrSting addAttribute:NSFontAttributeName value:secondFont range:NSMakeRange(firstLength, secondLength)];
    [attrSting addAttribute:NSForegroundColorAttributeName value:firstColor range:NSMakeRange(0, firstLength)];
    [attrSting addAttribute:NSForegroundColorAttributeName value:secondColor range:NSMakeRange(firstLength, secondLength)];
    
    return attrSting;
}

/** 两个关键字同颜色同字号 */
+ (NSMutableAttributedString *)ym_setAttributes:(NSString *)normalStr normalFont:(UIFont *)normalFont normalColor:(UIColor *)normalColor firstSpecialStr:(NSString *)firstSpecialStr :(NSString *)secondSpecialStr specialFont:(UIFont *)specialFont specialColor:(UIColor *)specialColor
{
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:normalStr];
    NSRange oneRange = [normalStr rangeOfString:firstSpecialStr];
    NSRange twoRange = [normalStr rangeOfString:secondSpecialStr];
    //整个字符串的颜色和字体
    [attrString addAttribute:NSFontAttributeName value:normalFont range:NSMakeRange(0, normalStr.length)];
    [attrString addAttribute:NSForegroundColorAttributeName value:normalColor range:NSMakeRange(0, normalStr.length)];
    //第一个特殊字符串的颜色和字体
    [attrString addAttribute:NSFontAttributeName value:specialFont range:oneRange];
    [attrString addAttribute:NSForegroundColorAttributeName value:specialColor range:oneRange];
    //第二个特殊字符串的颜色和字体
    [attrString addAttribute:NSFontAttributeName value:specialFont range:twoRange];
    [attrString addAttribute:NSForegroundColorAttributeName value:specialColor range:twoRange];
    
    return attrString;
    
}

/** 两个关键字不同颜色不字号 */
+ (NSMutableAttributedString *)ym_setAttributes:(NSString *)normalStr normalFont:(UIFont *)normalFont normalColor:(UIColor *)normalColor firstSpecialStr:(NSString *)firstSpecialStr :(NSString *)secondSpecialStr firstSpecialFont:(UIFont *)firstSpecialFont firstSpecialColor:(UIColor *)firstSpecialColor secondSpecialFont:(UIFont *)secondSpecialFont secondSpecialColor:(UIColor *)secondSpecialColor
{
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:normalStr];
    NSRange oneRange = [normalStr rangeOfString:firstSpecialStr];
    NSRange twoRange = [normalStr rangeOfString:secondSpecialStr];
    //整个字符串的颜色和字体
    [attr addAttribute:NSFontAttributeName value:normalFont range:NSMakeRange(0, normalStr.length)];
    [attr addAttribute:NSForegroundColorAttributeName value:normalColor range:NSMakeRange(0, normalStr.length)];
    //第一个特殊字符串的颜色和字体
    [attr addAttribute:NSFontAttributeName value:firstSpecialFont range:oneRange];
    [attr addAttribute:NSForegroundColorAttributeName value:firstSpecialColor range:oneRange];
    //第二个特殊字符串的颜色和字体
    [attr addAttribute:NSFontAttributeName value:secondSpecialFont range:twoRange];
    [attr addAttribute:NSForegroundColorAttributeName value:secondSpecialColor range:twoRange];
    
    return attr;
}


@end
